<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">

            <!-- 放置表单 -->
            <el-form ref="userForm" label-width="120px" :model="userinfo" :rules="rules" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userinfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password">
                <el-input v-model="userinfo.password" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="update">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <userinfo />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <jobinfo />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getuserid } from '../../api/user'
import { saveUserDetailById } from '../../api/employees'

// 引入组件
import userinfo from './components/user-info.vue'
import jobinfo from './components/job-info.vue'
export default {
  components: {
    userinfo,
    jobinfo
  },
  data() {
    return {
      userId: this.$route.params.id,
      userinfo: {},
      rules: {
        username: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    // 把整个请求回来的数据都赋值给我们定义的 userinfo , userinfo里面不管写什么都会被我请求回来的数据给覆盖,请求回来的数据,里面自带有 username 和 password ,所有可以直接使用 userinfo.username与userinfopassword
    const res = await getuserid(this.userId)
    // 把密码变为空,因为不能直接修改
    this.userinfo = {
      ...res,
      password: ''
    }
    console.log(43, 'userinfo', this.userinfo)
  },
  methods: {
    async  update() {
      // 校验
      this.$refs.userForm.validate()
      // 发起修改请求
      // 需要传入id
      await saveUserDetailById(this.userinfo)
      // 提示用户
      this.$message.success('修改成功')
    }
  }
}
</script>

<style>

</style>
